package site.sorghum.ddl.solon.config;

import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Condition;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import site.sorghum.ddl.mybatis.plus.MybatisPlusDdlWrapper;
import site.sorghum.ddl.mybatis.plus.MybatisPlusEntityGenerate;
import site.sorghum.ddl.mybatis.plus.MybatisPlusInitConfig;
import site.sorghum.ddl.solon.prop.GlobalConfigProp;

import javax.sql.DataSource;

@Configuration
@org.springframework.context.annotation.Configuration
@Condition(onClass = MybatisPlusInitConfig.class)
@ConditionalOnClass(value = MybatisPlusInitConfig.class)
public class PlusConfig {

    @Bean
    @org.springframework.context.annotation.Bean
    MybatisPlusEntityGenerate mybatisPlusEntityGenerate() {
        return new MybatisPlusEntityGenerate();
    }


    @Bean
    @org.springframework.context.annotation.Bean
    MybatisPlusDdlWrapper mybatisPlusDdlWrapper(@Autowired @Inject DataSource dataSource) {
        return new MybatisPlusDdlWrapper(dataSource);
    }


    @Bean
    @org.springframework.context.annotation.Bean
    MybatisPlusInitConfig mybatisPlusInitConfig(@Autowired(required = false) @Inject(required = false) GlobalConfigProp globalConfig,
                                                @Autowired @Inject MybatisPlusDdlWrapper mybatisPlusDdlWrapper, @Autowired @Inject MybatisPlusEntityGenerate mybatisPlusEntityGenerate) {
        if (globalConfig != null) {
            globalConfig.init();
        }
        return new MybatisPlusInitConfig(mybatisPlusDdlWrapper, mybatisPlusEntityGenerate);
    }
}
